package com.chaidb;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.db.DbConnection;

public class SharePhoto {
	@SuppressWarnings({ "finally", "unchecked" })
	public String doSharePhoto(String photoid,String userid)
	{
		String result = null;
		System.out.println("I am here in share photo");
		String querysql = "select * from photo_share where pid='" +
			photoid + "' && uid='" + userid  + "';";
		System.out.println(querysql);
		String createsql = "insert into photo_share(pid,uid)values('" +
				photoid + "','" + userid  + "');";
		System.out.println(createsql);
				
		Connection con = null;
		try {
			con = DbConnection.getInstance().getConnection();
			Statement st = con.createStatement();
			Statement st2 = con.createStatement();
			ResultSet rs1 = st.executeQuery(querysql);
			if(rs1.next())
			{
				result = "exist";
			}
			else
			{
				int rs = st2.executeUpdate(createsql);
				if(rs == 1)
				{
					result = "successful";
				}
			}
		}catch(SQLException e)
		{
			e.printStackTrace();
		}finally
		{
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return result;
		}
	}


}
